package com.rongchuang.service;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.rongchuang.dto.MenuDTO;
import com.rongchuang.entity.Menu;
import com.rongchuang.entity.QMenu;
import com.rongchuang.repository.MenuRepository;
import com.rongchuang.util.ConverterUtil;
import com.rongchuang.util.DateUtils;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class MenuService {

    @Autowired
    private MenuRepository menuRepository;

    @Autowired
    private JPAQueryFactory queryFactory;

    /**
     * 分页查询
     */
    public Page<MenuDTO> pageSearch(int pageNum, int pageSize, String name) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        QMenu menu = QMenu.menu;
        BooleanBuilder builder = new BooleanBuilder();
        if (StringUtils.isNotBlank(name)) {
            builder.and(menu.menuName.eq(name));
        }
        List<Menu> menuList = queryFactory
                .selectFrom(menu)
                .where(builder)
                .offset((long) pageNum * pageSize)
                .limit(pageSize)
                .orderBy(menu.id.desc())
                .fetch();
        long total = queryFactory
                .selectFrom(menu)
                .where(builder)
                .fetchCount();
        List<MenuDTO> menuDTOS = ConverterUtil.convertToList(menuList, MenuDTO.class);
        return new PageImpl<>(menuDTOS, pageable, total);
    }

    /**
     * 保存菜单信息
     */
    public void modify(MenuDTO menuDTO) {
        Menu menu;
        if (Objects.isNull(menuDTO.getId())) {
            menu = ConverterUtil.convertTo(menuDTO, Menu.class);
            menu.setCreateTime(DateUtils.getCurrentTime());
            menu.setParentId(0L);
        }else {
            menu = menuRepository.getById(menuDTO.getId());
            if (StringUtils.isNotBlank(menuDTO.getMenuName())) {
                menu.setMenuName(menuDTO.getMenuName());
            }
            if (StringUtils.isNotBlank(menuDTO.getUrl())) {
                menu.setUrl(menuDTO.getUrl());
            }
            if (Objects.nonNull(menuDTO.getParentId())) {
                menu.setParentId(menuDTO.getParentId());
            }
            if (Objects.nonNull(menuDTO.getPermissionId())) {
                menu.setPermissionId(menuDTO.getPermissionId());
            }
        }
        menu.setUpdateTime(DateUtils.getCurrentTime());
        menuRepository.save(menu);
    }

    /**
     * 获取菜单详情
     * @param id 菜单id
     * @return 菜单详情
     */
    public MenuDTO getDetail(Long id) {
        Menu menu = menuRepository.getById(id);
        return ConverterUtil.convertTo(menu, MenuDTO.class);
    }
}
